﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using System.Web;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;
using System.Net.Sockets;
using System.Drawing;

namespace Tickz.Commonz
{
    public class ValidateExt
    {
        #region 常量及变量
        /// <summary>
        /// 匹配是否中文和全角特殊符号的正则表达式
        /// </summary>
        public const string ContainChineseRegexString = "[^\x00-\x80\uFE30-\uFFA0]+";
        /// <summary>
        /// 匹配全部都是中文和全角特殊符号的正则表达式
        /// </summary>
        public const string IsChineseRegexString = "^[^\x00-\x80\uFE30-\uFFA0]+$";

        #endregion

        #region 常见验证
        /// <summary>
        /// 验证字符串是否电子邮件
        /// </summary>
        /// <param name="content"></param>
        public static bool IsMail(string content)
        {
            if (content != null)
                return Regex.IsMatch(content.Trim(), @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            return false;
        }
        /// <summary>
        /// 检测是否符合email格式
        /// </summary>
        /// <param name="strEmail">要判断的email字符串</param>
        /// <returns>判断结果</returns>
        //public static bool IsValidEmail(string strEmail)
        //{
        //    return Regex.IsMatch(strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");
        //}

        //public static bool IsValidDoEmail(string strEmail)
        //{
        //    return Regex.IsMatch(strEmail, @"^@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        //}
        /// <summary>
        /// 验证字符串是否国内电话
        /// </summary>
        /// <param name="content"></param>
        public static bool IsPhone(string content)
        {
            if (content != null)
                return Regex.IsMatch(content.Trim(), @"^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$", RegexOptions.Compiled);
            return false;
        }
        /// <summary>
        /// 是否身份证号码
        /// </summary>
        /// <param name="strID"></param>
        /// <returns></returns>
        public static bool IsIDCard(string strID)
        {
            if (!String.IsNullOrEmpty(strID))
                return Regex.IsMatch(strID.Trim(), @"^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|71|(8[12])|91)\d{4}((19\d{2}(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(19\d{2}(0[13578]|1[02])31)|(19\d{2}02(0[1-9]|1\d|2[0-8]))|(19([13579][26]|[2468][048]|0[48])0229))\d{3}(\d|X|x)?$", RegexOptions.Compiled);
            return false;
        }
        /// <summary>
        /// 是否QQ号码
        /// </summary>
        /// <param name="strQQ"></param>
        /// <returns></returns>
        public static bool IsQQ(string strQQ)
        {
            if (!String.IsNullOrEmpty(strQQ))
                return Regex.IsMatch(strQQ.Trim(), @"^[1-9]*[1-9][0-9]*$", RegexOptions.Compiled);
            return false;
        }
       

        #endregion
        
        #region 类型验证
        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(object expression)
        {
            if (expression != null)
                return IsNumeric(expression.ToString());

            return false;

        }
        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(string expression)
        {
            if (expression != null)
            {
                string str = expression;
                if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
                {
                    if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                        return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 判断该字符串只有数字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumericOnly(string input)
        {
            return Regex.IsMatch(input, @"^\d+$");
        }
        /// <summary>
        /// 判断该字符串中只有没有0的数字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumericNotZero(string input)
        {
            return Regex.IsMatch(input, @"^[^0]\d*$");
        }
        /// <summary>
        /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
        /// </summary>
        /// <param name="strNumber">要确认的字符串数组</param>
        /// <returns>是则返加true 不是则返回 false</returns>
        public static bool IsNumericArray(string[] strNumber)
        {
            if (strNumber == null)
                return false;

            if (strNumber.Length < 1)
                return false;

            foreach (string id in strNumber)
            {
                if (!IsNumeric(id))
                    return false;
            }
            return true;
        }
        /// <summary>
        /// 判断是不是英文字母
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsLetters(string input)
        {
            return Regex.IsMatch(input, @"^\w+$");
        }
        /// <summary>
        /// 判断字符串是不是包含A-Za-z0-9_
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsLetterAndNumber(string input)
        {
            return Regex.IsMatch(input, "^[A-Za-z0-9_]+$");
        }
       
        /// <summary>
        /// 是否为Double类型
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsDouble(object expression)
        {
            if (expression != null)
                return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");

            return false;
        }
       
        /// <summary>
        /// 判断对象是否为空 如果字符串为"" 返回true
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool IsNull(Object obj)
        {
            if (obj == null)
            {
                return true;
            }
            if (obj is String)
            {
                return string.IsNullOrEmpty(obj.ToString());
            }
            return false;

        }
        /// <summary>
        /// 字段串是否为Null或为""(空) 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(string str)
        {
            if (str == null || str.Trim() == string.Empty)
                return true;

            return false;
        }
        /// <summary>
        /// 判断是不是日期字符串
        /// </summary>
        /// <param name="dateStr"></param>
        /// <returns></returns>
        public static bool IsDate(string dateStr)
        {
            try
            {
                DateTime.Parse(dateStr);
                return true;
            }
            catch
            {
                return false;
            }
        }
       
        #endregion

        #region 网络验证
        /// <summary>
        /// 是否IP地址(IP4)
        /// </summary>
        /// <param name="strIP"></param>
        /// <returns></returns>
        public static bool IsIPAddr(string strIP)
        {
            if (!String.IsNullOrEmpty(strIP))
                return Regex.IsMatch(strIP.Trim(), @"^([1-9]|[1-9]\d|1\d{2}|2[0-1]\d|22[0-3])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}$", RegexOptions.Compiled);
            return false;
        }
        /// <summary>
        /// 是否IPV6版本地址
        /// </summary>
        /// <param name="strIP"></param>
        /// <returns></returns>
        public static bool IsIPV6Addr(string strIP)
        {
            try
            {
                IPAddress ipAddr = IPAddress.Parse(strIP);
                if (ipAddr.AddressFamily == AddressFamily.InterNetworkV6)
                    return true;
                return false;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 是否网卡MAC地址
        /// </summary>
        /// <param name="strMAC"></param>
        /// <returns></returns>
        public static bool IsMACAddr(string strMAC)
        {
            if (!String.IsNullOrEmpty(strMAC))
                return Regex.IsMatch(strMAC.Trim(), @"^[A-Fa-f\d]{2}-[A-Fa-f\d]{2}-[A-Fa-f\d]{2}-[A-Fa-f\d]{2}-[A-Fa-f\d]{2}-[A-Fa-f\d]{2}$", RegexOptions.Compiled);
            return false;
        }


        /// <summary>
        /// 检测是否是正确的Url
        /// </summary>
        /// <param name="strUrl">要验证的Url</param>
        /// <returns>判断结果</returns>
        public static bool IsURL(string strUrl)
        {
            return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
        }
        ///// <summary>
        ///// 是否网络地址
        ///// </summary>
        ///// <param name="strURL"></param>
        ///// <returns></returns>
        //public static bool IsURL(string strURL)
        //{
        //    if (!strURL.IsNullOrEmpty(strURL))
        //        return Regex.IsMatch(strURL.Trim(), @"^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*[\.。])+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|([1-9]|[1-9]\d|1\d{2}|2[0-1]\d|22[0-3])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3})|(:\d+)(\/[\w-.\/?%&=]*)?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        //    return false;
        //}

        #endregion

        #region 安全
        /// <summary>
        /// 是否包含危险的SQL注入语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static bool IsSafeSQL(string sql)
        {
            return !Regex.IsMatch(sql.Trim(), ".*(and |drop |exec |insert |select |delete |update |count |or |%|:|'|\"|chr|mid|master |truncate|char|declare |SiteName|net user|xp_cmdshell|/add|exec master.dbo.xp_cmdshell|net localgroup administrators).*", RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 判断是否为base64字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsBase64String(string str)
        {
            //A-Z, a-z, 0-9, +, /, =
            return Regex.IsMatch(str.Trim(), @"[A-Za-z0-9\+\/\=]");
        }
        ///// <summary>
        ///// 判断是否为base64字符串
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //public static bool IsBase64String(string str)
        //{
        //    //A-Z, a-z, 0-9, +, /, =
        //    return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
        //}

        /// <summary>
        /// 检测是否有Sql危险字符
        /// </summary>
        /// <param name="str">要判断字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsSafeSqlString(string str)
        {
            return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
        }

        /// <summary>
        /// 检测是否有危险的可能用于链接的字符串
        /// </summary>
        /// <param name="str">要判断字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsSafeUserInfoString(string str)
        {
            return !Regex.IsMatch(str, @"^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|游客|^Guest");
        }
        #endregion
       

       
    }
}
